package com.xfzcode.controller;

import com.xfzcode.enums.ResultEnum;
import com.xfzcode.enums.TipsEnum;
import com.xfzcode.pojo.CommonEntity;
import com.xfzcode.result.ResponseData;
import com.xfzcode.service.ElasticsearchDocumentService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.rest.RestStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.naming.directory.SearchResult;
import java.util.List;

/**
 * @Author XMLee
 * @Date 2022年08月11日 22:42
 * @Description:
 */
@RequestMapping("/v1/docs")
@RestController
public class ElasticsearchDocController {
    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchDocController.class);

    @Autowired
    private ElasticsearchDocumentService elasticsearchDocumentService;

    /**
     * 全文检索
     *
     * @param commonEntity
     * @return
     */
    @GetMapping("/matchQuery")
    public ResponseData matchQuery(@RequestBody CommonEntity commonEntity) {
        //构造返回数据
        ResponseData resultData = new ResponseData();

        //es 批量查询返回结果
        SearchResponse result = null;
        try {
            //通过高阶API调用批量查询操作方法
            result = elasticsearchDocumentService.matchQuery(commonEntity);
            //查询数量除以每页数量 等于合计分页数量
            long aSize = result.getHits().getTotalHits().value;
            logger.info("总数据量:" + aSize + "条");
            int cSize = result.getHits().getHits().length;
            logger.info("当前获取数据:" + cSize + "条");
            //通过类型推断自动装箱（多个参数取交集）
            resultData.setResultEnum(result.getHits().getHits(), ResultEnum.success, Integer.valueOf(String.valueOf(aSize)));
            //日志记录
            logger.info(TipsEnum.batch_get_doc_success.getMessage());
        } catch (Exception e) {
            //打印到控制台
            e.printStackTrace();
            // 日志记录
            logger.error(TipsEnum.batch_get_doc_fail.getMessage());
            //构建错误返回信息
            resultData.setResultEnum(ResultEnum.error);
        }
        return resultData;
    }

    /**
     * 结构话搜索
     * @param commonEntity
     * @return
     */
    @GetMapping("/termQuery")
    public ResponseData termQuery(@RequestBody CommonEntity commonEntity) {
        //构造返回数据
        ResponseData resultData = new ResponseData();

        //es 批量查询返回结果
        SearchResponse result = null;
        try {
            //通过高阶API调用批量查询操作方法
            result = elasticsearchDocumentService.termQuery(commonEntity);
            //查询数量除以每页数量 等于合计分页数量
            long aSize = result.getHits().getTotalHits().value;
            logger.info("总数据量:" + aSize + "条");
            int cSize = result.getHits().getHits().length;
            logger.info("当前获取数据:" + cSize + "条");
            //通过类型推断自动装箱（多个参数取交集）
            resultData.setResultEnum(result.getHits().getHits(), ResultEnum.success, Integer.valueOf(String.valueOf(aSize)));
            //日志记录
            logger.info(TipsEnum.batch_get_doc_success.getMessage());
        } catch (Exception e) {
            //打印到控制台
            e.printStackTrace();
            // 日志记录
            logger.error(TipsEnum.batch_get_doc_fail.getMessage());
            //构建错误返回信息
            resultData.setResultEnum(ResultEnum.error);
        }
        return resultData;
    }

    /**
     * 自动补全
     * @param commonEntity
     * @return
     */
    @GetMapping(value = "/csuggest")
    public ResponseData cSuggest(@RequestBody CommonEntity commonEntity) {
        ResponseData responseData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            responseData.setResultEnum(ResultEnum.param_isnull);
            return responseData;
        }
        //定义批量查询返回结构
        List<String> result = null;
        try {
            result = elasticsearchDocumentService.cSuggest(commonEntity);
            //通过类型推断自动装箱
            responseData.setResultEnum(result, ResultEnum.success, result.size());
            //日志记录
            logger.info(TipsEnum.csuggest_get_doc_success.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.csuggest_get_doc_fail.getMessage());
            responseData.setResultEnum(ResultEnum.error);
        }
        return responseData;
    }

    /**
     * 批量导入映射
     * @param commonEntity
     * @return
     */
    @PostMapping("/batch")
    public ResponseData bulkAdnDoc(@RequestBody CommonEntity commonEntity) {
        //构造返回数据
        ResponseData responseData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || CollectionUtils.isEmpty(commonEntity.getList())) {
            responseData.setResultEnum(ResultEnum.param_isnull);
            return responseData;
        }
        RestStatus result = null;
        try {
            result = elasticsearchDocumentService.bulkAndDoc(commonEntity);
            //通过类型推断自动装箱
            responseData.setResultEnum(result, ResultEnum.success, null);
            logger.info(TipsEnum.batch_create_doc_success.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(TipsEnum.batch_create_doc_success.getMessage());
            responseData.setResultEnum(ResultEnum.error);
        }
        return responseData;
    }

    /**
     * 拼音纠错
     * @param commonEntity
     * @return
     */
    @GetMapping(value = "/parseSuggest")
    public ResponseData parseSuggest(@RequestBody CommonEntity commonEntity) {
        ResponseData responseData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            responseData.setResultEnum(ResultEnum.param_isnull);
            return responseData;
        }
        //定义返回纠错结果
        String result = null;
        try {
            result = elasticsearchDocumentService.parseSuggest(commonEntity);
            //通过类型推断自动装箱
            responseData.setResultEnum(result, ResultEnum.success, null);
            //日志记录
            logger.info(TipsEnum.psuggest_get_doc_success.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.psuggest_get_doc_fail.getMessage());
            responseData.setResultEnum(ResultEnum.error);
        }
        return responseData;
    }

    @GetMapping(value = "/tsuggest")
    public ResponseData tSuggest(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData responseData = new ResponseData();
        //判断参数
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            responseData.setResultEnum(ResultEnum.param_isnull);
            return responseData;
        }
        //定义返回推荐结果
        String result = null;
        try {
            //调用纠错方法
            result = elasticsearchDocumentService.tSuggest(commonEntity);
            //通过类型推断自动装箱
            responseData.setResultEnum(result, ResultEnum.success, null);
            //日志记录
            logger.info(TipsEnum.tsuggest_get_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.tsuggest_get_doc_fail.getMessage());
            responseData.setResultEnum(ResultEnum.error);
        }
        //返回
        return responseData;
    }
















}
